package com.cn.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;

import com.cn.bean.Resident_collect;
import com.cn.dao.ResidentDAO;

public class ResidentDAOImpl extends BaseHibernateDAOImpl implements ResidentDAO {

	// 根据登记号查找居�?
	public Object findByAccount(String account) {
		try {
			String queryString = "from Resident as resident where account= '" + account + "'";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			getSession().getTransaction().commit();
			if (list.size() == 0) {
				return null;
				
			}
			return list.get(0);
			
		} catch (RuntimeException re) {
			System.out.println("re");
			throw re;
		}
	}

	// 根据手机号查找居�?
	public Object findByPhone(String phone) {
		try {
			String queryString = "from Resident as resident where phone= '" + phone +"'";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			getSession().getTransaction().commit();
			if (list.size() == 0) {
				return null;
			}
			return list.get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据id查找resident
	 * @param id
	 * @return resident对象
	 */
	public Object findById(int id) {
		try {
			String queryString = "from Resident as resident where id =" + id;
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			getSession().getTransaction().commit();
			if (list.size() == 0) {
				return null;
			}
			return list.get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public Object findCollect(int re_id,int pro_id)
	{
		try {
			String queryString = "from Resident_collect as rc where re_id =" + re_id +" and pro_id="+pro_id;
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			getSession().getTransaction().commit();
			if (list.size() == 0) {
				return null;
			}
			return list.get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public boolean deleteCollection(int re_id,int pro_id)
	{
		try {
			String queryString = "from Resident_collect as rc where re_id =" + re_id +" and pro_id="+pro_id;
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			getSession().getTransaction().commit();
			if (list.size() == 0) {
				return true;
			}
			Resident_collect rc=(Resident_collect)list.get(0);
			return delete(rc);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
}
